package com.dididoctor.patient.Activity.Service.ServiceDetail;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Doctor.Doctordetail.Prdbuy;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

/**
 * Created by Administrator on 2016/4/16.
 */
public class ServiceDetailPresenter extends BasePresenter {

    private ServiceDetailView view;

    public ServiceDetailPresenter(Context context, ServiceDetailView view) {
        super(context, view);
        this.view = view;
    }

    /**
     * 获得服务详情
     *
     * @param prdId
     */
    public void getServicedetail(String prdId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("prdId", prdId);
        BusinessClient.post(ConstantValue.prdetail, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    ServiceDetail serviceDetail = new ServiceDetail();
                    serviceDetail.setPrdId(response.getString("prdId"));
                    serviceDetail.setName(response.getString("name"));
                    serviceDetail.setDescr(response.getString("descr"));
                    serviceDetail.setPrize((Double) response.getObject("prize") + "");
                    serviceDetail.setWebUrl(response.getString("webUrl"));
                    serviceDetail.setIsallow(response.getString("isallow"));
                    view.getServiceDetail(serviceDetail);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getServiceFail();
            }
        });
    }

    /**
     * 下单产品
     *
     * @param prdId
     * @param price
     * @param ptype
     * @param type
     * @param recId
     * @param doctorId
     * @param hosId
     */
    void prdbuy(String prdId, String price, String ptype, String type, String recId, String doctorId, String hosId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("prdId", prdId);
        params.put("price", price);
        params.put("ptype", ptype);
        params.put("type", type);
        params.put("recId", recId);
        params.put("doctorId", doctorId);
        params.put("hosId", hosId);
        params.put("payWay", "1");
        BusinessClient.post(ConstantValue.prdbuy, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Prdbuy prdbuy = new Prdbuy();
                    prdbuy.setPayId(Util.toString(response.getString("payId")));
                    prdbuy.setSign(Util.toString(response.getString("sign")));
                    prdbuy.setNonceStr(Util.toString(response.getString("nonceStr")));
                    prdbuy.setDescr(Util.toString(response.getString("descr")));
                    prdbuy.setNotifyurl(Util.toString(response.getString("notifyurl")));
                    prdbuy.setTimeStamp(Util.toString(response.getString("timeStamp")));
                    prdbuy.setPartnerId(Util.toString(response.getString("partnerId")));
                    prdbuy.setPrepayId(Util.toString(response.getString("prepayId")));
                    prdbuy.set_input_charset(Util.toString(response.getString("_input_charset")));
                    prdbuy.setSubject(Util.toString(response.getString("subject")));
                    prdbuy.setTotal_fee(Util.toString(response.getString("total_fee")));
                    prdbuy.setSign_type(Util.toString(response.getString("sign_type")));
                    prdbuy.setService(Util.toString(response.getString("service")));
                    prdbuy.setNotify_url(Util.toString(response.getString("notify_url")));
                    prdbuy.setPartner(Util.toString(response.getString("partner")));
                    prdbuy.setSeller_id(Util.toString(response.getString("seller_id")));
                    prdbuy.setOut_trade_no(Util.toString(response.getString("out_trade_no")));
                    prdbuy.setPayment_type(Util.toString(response.getString("payment_type")));
                    prdbuy.setPrivateKey(Util.toString(response.getString("privateKey")));
                    view.prdbuy(prdbuy);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.prdbuyfail();
            }
        });
    }

    public void paysuccess(String type, String payId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.paysuccess, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.paysuccess();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
            }
        });
    }
}
